package com.sc.nft.service.impl;

import com.sc.nft.dao.MallBillStatusLogDao;
import com.sc.nft.dao.SysMemberDao;
import com.sc.nft.entity.MallBillStatusLog;
import com.sc.nft.entity.SysMember;
import com.sc.nft.entity.dto.MallBillStatusLogDTO;
import com.sc.nft.enums.MallBillStatusEnum;
import com.sc.nft.pool.SysMemberPool;
import com.sc.nft.service.MallBillStatusLogService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import java.util.Objects;

/**
 * <p>
 * 账单状态操作日志表 服务实现类
 * </p>
 *
 * @author ligen
 * @since 2024-01-17
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class MallBillStatusLogServiceImpl implements MallBillStatusLogService {

    private final MallBillStatusLogDao mallBillStatusLogDao;
    private final SysMemberDao sysMemberDao;

    /**
     * 创建账单状态日志
     *
     * @param billId
     * @param statusEnum
     * @param remark
     */
    @Override
    public void create(Long billId, MallBillStatusEnum statusEnum, String remark) {
        MallBillStatusLog mallBillStatusLog = new MallBillStatusLog();
        mallBillStatusLog.setOrderBillId(billId);
        mallBillStatusLog.setChangeType(statusEnum.name());
        mallBillStatusLog.setChangeMessage(statusEnum.getDescription());
        mallBillStatusLog.setRemark(remark);
        mallBillStatusLog.setCreateId(SysMemberPool.getMember().getId());
        mallBillStatusLog.insert();
    }

    /**
     * 获取账单状态日志
     *
     * @param billId
     * @return
     */
    @Override
    public MallBillStatusLogDTO getTheLatestStatus(Long billId) {
        MallBillStatusLog theLatestStatus = mallBillStatusLogDao.getTheLatestStatus(billId);
        if (Objects.nonNull(theLatestStatus)){
            MallBillStatusLogDTO mallBillStatusLogDTO = new MallBillStatusLogDTO();
            BeanUtils.copyProperties(theLatestStatus,mallBillStatusLogDTO);
            if (Objects.nonNull(theLatestStatus.getCreateId()) && MallBillStatusEnum.sysUserList().contains(theLatestStatus.getChangeType())){
                SysMember sysMember = sysMemberDao.getById(theLatestStatus.getCreateId());
                if (Objects.nonNull(sysMember)){
                    mallBillStatusLogDTO.setAuditName(sysMember.getUserName());
                }
            }
            return mallBillStatusLogDTO;
        }
        return null;
    }


}
